Skip to content

fix(prime-random): replace poisson_disk_2d with O(1) poisson_disk#43

Open
bwyard wants to merge 1 commit into
devfrom
fix/bridson-perf
Open

fix(prime-random): replace poisson_disk_2d with O(1) poisson_disk#43
bwyard wants to merge 1 commit into
devfrom
fix/bridson-perf

Conversation

@bwyard

@bwyard bwyard commented Apr 6, 2026

Copy link
Copy Markdown
Owner

Summary

  • poisson_disk_2d cloned the full spatial grid (O(cols×rows)) on every accepted point via the pure-fold successors chain — a hidden performance bug that got worse non-linearly with domain size
  • New poisson_disk uses internal mutation with O(1) grid access; external contract stays pure (same seed → same output)
  • Signature cleaned up: seed moved to last arg, no seed returned
  • WASM binding updated to match

Benchmark (min_dist=5.0, seed=42, k=30)

Domain Old (poisson_disk_2d) New (poisson_disk) Speedup
50×50 221.6 µs 162.1 µs 1.4×
100×100 1249.8 µs 696.2 µs 1.8×
200×200 7594 µs 2874 µs 2.6×
500×500 157.5 ms 18.2 ms 8.6×

Speedup is non-linear because grid clone cost scales with domain area while the fix is O(1).

Test plan

  • cargo test -p prime-random — 27 tests pass
  • cargo build -p prime-wasm — builds clean
  • Verify same output: poisson_disk(100.0, 100.0, 10.0, 30, 42) produces same points as old poisson_disk_2d(42, 100.0, 100.0, 10.0, 30)

🤖 Generated with Claude Code

The old poisson_disk_2d cloned the entire spatial grid (O(cols×rows))
on every accepted point via the pure-fold successors chain. At 500×500
with min_dist=5, that's ~40,000 clones per step — 8.6× slower than
necessary.

The new poisson_disk uses internal mutation with O(1) grid access while
keeping the external contract pure (same seed → same output, always).
Signature simplified: seed moved to last arg, no seed returned (pure
functions don't thread state out).

- Remove BridsonState/BridsonParams/bridson_step infrastructure (fold-only)
- Add poisson_disk(width, height, min_dist, max_attempts, seed) -> Vec
- Update all unit tests; replace poisson_disk_returns_seed with
  poisson_disk_empty_on_invalid_input (new guard tested)
- Update bench: add 500×500 size, rename group to poisson_disk
- Update prime-wasm binding to match new signature

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant